package cn.yunhe.view;


import cn.yunhe.model.Person;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;


/**
 * @author xu yuanjun
 * @data 2019/4/1 10:36
 * java 1.8 的 collectors   grouping
 */
public class Collectorstion {

    public static final String[] eating = new String[]{
      "apple",
      "apple",
      "banana",
      "apple",
      "orange",
      "banana",
      "papaya"
    };

    /**
     *对list的数据进行分组并求其总数
     */
    public static void grounpBy(){
        List<String> items = Arrays.asList(eating);
        Map<String, Long> result =
                items.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
        System.out.println(result);
    }

    /**
     * 对list增加排序
     */
    public static void addPaiXu(){
        List<String> items = Arrays.asList(eating);
        Map<String, Long> result =
                items.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

        Map<String, Long> finalMap = new LinkedHashMap<>();
        //Sort a map and add to finalMap
        result.entrySet().stream().sorted(
                Map.Entry.<String, Long>comparingByKey().reversed()
        ).forEachOrdered(e->finalMap.put(e.getKey(), e.getValue()));

        System.out.println(finalMap);
    }

    public static void zuHe() {
        Person[] person = new Person[6];
        person[0] = new Person(1, 23, "许元军");
        person[1] = new Person(1, 22, "许");
        person[2] = new Person(1, 21, "元");
        person[3] = new Person(1, 20, "军");
        person[4] = new Person(1, 21, "军");
        person[5] = new Person(1, 20, "许元军");
        List<Person> items = Arrays.asList(person);
        //以某个字段分组并且计数求和
        Map<String, Long> counting = items.stream()
                .collect(Collectors.groupingBy(Person::getName, Collectors.counting()));
       //以某个字段分组，并计算另一个字段的分组之后的和
        Map<String, Integer> sum = items.stream().collect(Collectors.groupingBy(Person::getName, Collectors.summingInt(Person::getAge)));
        System.out.println(counting);
        System.out.println(sum);
    }

    public static void group() {
        Person[] person = new Person[6];
        person[0] = new Person(1, 23, "许元军");
        person[1] = new Person(2, 22, "许");
        person[2] = new Person(3, 21, "元");
        person[3] = new Person(4, 20, "军");
        person[4] = new Person(4, 21, "军");
        person[5] = new Person(1, 20, "许元军");
        List<Person> items = Arrays.asList(person);
        //以某个字段分组得到Map类型的集合
        Map<Integer, List<Person>> objectPerson = items.stream()
                .collect(Collectors.groupingBy(Person::getAge));

     /*  Map<Integer, Set<String>> result = items.stream()
               .collect(Person::getId, Collectors.mapping(Person::getName, Collectors.toSet()));
*/

       //取出某一个List对象中某一属性的列表
        /*items.stream().filter(x->x.getId()==4).distinct().map(Person::getId).collect(Collectors.toList()).stream()*/

        System.out.println(objectPerson);


    }



    public static void main(String[] args) {
        group();

    }


}
